package edu.faytechcc.csc.business;

import java.awt.Toolkit;
import java.io.File;
import javax.swing.SwingWorker;
 
/**
 * This class contains the algorithms needed to edit the Alice class files 
 * based on the information given to the Model. It is a SwingWorker so the
 * when convert() is called in a separate thread, the progress of the 
 * Algorithm will be updated allowing for progress bars and such to be used.
 * 
 * @author Charles Bryan
 */
public class Controller extends SwingWorker<Void, Void>
{

    private Model model;

    /**
     * Creates a new Controller object given a Model that will be processed.
     * 
     * @param model The model to be processed.
     */
    public Controller(Model model)
    {
        this.model = model;
    }

    /**
     * The algorithm used to process through all students and all Alice classes
     * in the model, editing each Alice classes elementData.xml file.
     */
    public void convert()
    {
        File destination;
        File aliceClassDir;

        //create a directory with the section name to place all of the 
        //processed zip files.
        File sectionDir =
                new File(model.getDestination() + "\\" + model.getSection());
        sectionDir.mkdir();

        //used to determine progress.
        double currentProgress = 0;
        //how much percentagewise is accomplished each time a Alice class is
        //processed.
        double interval = 100.0
                / (model.getAliceClasses().size() * model.getStudents().size());
        //if single student and single class
        interval = (interval == 100) ? 50 : interval;

        //process all students in the model
        for (String student : model.getStudents())
        {
            //create a directory with the student name to place all of the 
            //processed Alice class files.
            destination =
                    new File(model.getDestination() + "\\" + student);
            destination.mkdir();

            //process all the Alice classes in the model
            for (String aliceClass : model.getAliceClasses())
            {
                String diff = "\\";
                if (!model.getDirectory().isEmpty())
                {
                    diff = aliceClass.replace(model.getDirectory(), "");
                    int index = diff.lastIndexOf("\\");
                    diff = diff.substring(0, index);
                }

                //create a directory with the Alice class name to place the  
                //contents of the a2c unzip
                aliceClassDir = new File(destination.getPath() + diff
                        + getAliceClassName(aliceClass));
                aliceClassDir.mkdir();

                //unzip the alice class
                FileUtilities.extract(aliceClass, aliceClassDir.getPath() + "\\");

                //edit the file
                FileUtilities.changeData(
                        aliceClassDir.getPath() + "\\" + "elementData.xml",
                        model.getSection(), student);

                //zip it back up into an a2c
                FileUtilities.compress(aliceClassDir.getPath() + ".a2c",
                        aliceClassDir.getPath());

                //delete the unziped files
                FileUtilities.deleteDirectory(aliceClassDir);
                
                //update the progress
                currentProgress += interval;
                setProgress((int) currentProgress);
            }
            
            //Create a file object to represent the student zip file
            File studentZip = 
                    new File(sectionDir.getPath() + "\\" + student + ".zip");

            //zip up all of the students new Alice classes
            FileUtilities.compress(studentZip.getPath(),
                    destination.getPath());
            
            //delete the student directory.
            FileUtilities.deleteDirectory(destination);
            
        }
        //all done!
        this.setProgress(100);
    }

    @Override
    public void done()
    {
        //Beep when the algorithm stops.
        Toolkit.getDefaultToolkit().beep();

        //report to all listening that the algorithm is done.
        this.firePropertyChange("done", 100, 100);
    }

    /**
     * Returns the Alice class name with out the file extension or the path 
     * name.
     * 
     * @param aliceClass the full path of a given Alice class
     * @return the name of the Alice class with out the path or .a2c extension
     */
    private String getAliceClassName(String aliceClass)
    {

        int index = aliceClass.lastIndexOf("\\");

        return aliceClass.substring(index, aliceClass.length() - 4);

    }

    @Override
    public Void doInBackground() throws Exception
    {
        //The algorithm has not started yet...
        setProgress(0);

        //start the Algorithm
        convert();

        //required for this method.
        return null;
    }
}
